import { BaseValue } from "../data/BaseValue";

const { ccclass, property } = cc._decorator;

@ccclass
export class Bullet extends cc.Component {

    @property({
        type: cc.Sprite
    })
    bullet: cc.Sprite = null;

    @property({
        type: cc.SpriteAtlas
    })
    bulletAtlas: cc.SpriteAtlas = null;

    private _attackValue = BaseValue.CANNON_LEVEL * 20;

    /**
     * 初始化子弹
     */
    public setEntry(startPos: cc.Vec2, endPos: cc.Vec2, degress: number) {
        this.bullet.spriteFrame = this.bulletAtlas.getSpriteFrame(`bullet_${BaseValue.CANNON_LEVEL}`);
        let name = this.bullet.spriteFrame.name;
        if (name.startsWith('net')) {
            console.log(name)
        }
        let rect = this.bullet.spriteFrame.getRect().size;
        this.node.setPosition(startPos);
        this.node.angle = degress;
        rect.width = rect.width / 3;
        rect.height = rect.height / 1.5;
        this.node.setContentSize(rect.height);
        this.node.getComponent(cc.BoxCollider).size = rect;

        cc.tween(this.node)
            .to(BaseValue.BULLET_SPEED, { position: endPos })
            .start();

    }

    /**
     * 子弹的碰撞检测
     * @param self 
     */
    collisionFish(self: cc.Collider) {
        this.node.stopAllActions();
        let collider = this.node.getComponent(cc.BoxCollider);
        cc.tween(self.node)
            .call(() => {
                this.bullet.spriteFrame = this.bulletAtlas.getSpriteFrame(`net_${BaseValue.CANNON_LEVEL}`);
            })
            .to(0.2, { scale: 1.2 })
            .call(() => {
                collider.enabled = false;
            })
            .call(() => {
                this.node.scale = 1;
                collider.enabled = true;
                this.node.destroy();
            })
            .start();
        return this._attackValue;
    }


}
